package com.workflow.engine.eb.steps.jd.login;

import com.workflow.engine.core.flow.StepState;
import com.workflow.engine.parser.annotation.PGConfig;
import com.workflow.engine.parser.annotation.PGScheme;
import com.workflow.engine.parser.constant.HttpMethod;
import com.workflow.engine.parser.flow.impl.AbstractStep;
import com.workflow.engine.parser.strategy.StrategyType;
import com.workflow.engine.parser.utils.StepStateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

import static com.workflow.engine.parser.constant.HttpMethod.GET;

/**
 * Created by Administrator on 2017/6/8.
 */
@PGConfig({
        @PGScheme(requestParamName = "a", strategy = StrategyType.FIXED, strategyNeedParams = "1"),
        @PGScheme(requestParamName = "acid", strategy = StrategyType.CONTEXT, strategyNeedParams = {"uuid", ""}),
        @PGScheme(requestParamName = "uid", strategy = StrategyType.CONTEXT, strategyNeedParams = {"uuid", ""}),
})
public class JDVerifyAuthCode extends AbstractStep {

    private static final Logger logger = LoggerFactory.getLogger(JDVerifyAuthCode.class);

    @Override
    public Logger getLogger() {
        return logger;
    }

    @Override
    protected void beforeSendRequest(Map<String, Object> context) {
        super.beforeSendRequest(context);
        setResponseIsStream(true);
    }

    @Override
    protected String getRequestUrl() {
        return "http://authcode.jd.com/verify/image";
    }

    @Override
    protected HttpMethod getHttpMethod() {
        return GET;
    }

    @Override
    protected Map<String, String> getHeaders() {
        return new HashMap<String, String>(){{
            put("Host", "authcode.jd.com");
            put("Proxy-Connection", "keep-alive");
            put("Cache-Control", "max-age=0");
            put("Upgrade-Insecure-Requests", "1");
            put("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.86 Safari/537.36");
            put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
            put("Accept-Encoding", "gzip, deflate");
            put("Accept-Language", "zh-CN,zh;q=0.8");
        }};
    }

    @Override
    protected StepState handleResponse(Map<String, Object> context, Object response) throws Exception {
        logger.info("识别图片验证码中");
        Scanner scanner = new Scanner(System.in);
        String authcode = scanner.nextLine();
        context.put("authcode", authcode);

        logger.info("识别图片验证码成功");
        logger.info("识别图片验证码失败");
        return StepStateUtil.successfulStepState(this);
    }
}
